﻿#pragma once
#include "common.h"

#include <QtWidgets/QMainWindow>
#include "ui_MyWindow.h"

// Qt includes.
//#include <QFileDialog>
//#include <QMessageBox>
//#include <QString>
//#include <QThread>
//#include <QTime>
//#include <QTimer>
//#include <QSlider>
//#include <QSpinBox>
//#include <QDoubleSpinBox>
//#include <QDial>
//#include <QPushButton>
//#include <QKeyEvent>
//#include <QToolTip>
//#include <QStringLiteral>
//#include <QTextCodec>
//#include <QWidget>
//#include <QLabel>
//#include <QEvent>
//#include <QHelpEvent>
//#include <QWheelEvent>
//#include <QVBoxLayout>

// include the MusicPlayer class.
#include "MusicPlayer.h"        // 包含音乐播放器类头文件
#include "CoordWindow.h"        // 包含坐标窗口类头文件
#include "QtMyThread.h"         // 包含线程类的头文件
#include "FFmpegTranscoding.h"  // 包含音频转码头文件
#include "TranscodeWindow.h"    // 包含音频转码 GUI 界面头文件
#include "OpenPCMWindow.h"      // 包含打开 PCM 音频文件窗口头文件
#include "RecordingWindow.h"    // 包含保存音频文件窗口头文件
//#include "VolumeControl.h"
//#include "CustomSlider.hpp"


QT_BEGIN_NAMESPACE
namespace Ui {
    class MyWindowClass;
};
QT_END_NAMESPACE

//static struct VarPos {
//    VarPos()
//        : x(0), y(0), z(0), flag_x(1), flag_y(1), flag_z(1) {}
//    VarPos(int my_x, int my_y, int my_z):
//        x(my_x), y(my_y), z(my_z), flag_x(1), flag_y(1), flag_z(1) {}
//    void set_pos(int set_x, int set_y, int set_z) {
//        x = set_x;
//        y = set_y;
//        z = set_z;
//    }
//    char flag_x, flag_y, flag_z;
//    int x, y, z;
//} var_listener_coord;


class MyWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MyWindow(QWidget *parent = nullptr);
    ~MyWindow();


private:
    void openPlayer(const std::string& fileName, bool is_pcm = false);

    void setInitStyle();

    void setOpenedStyle(bool reset = true);

    template<typename T>
    void run_in_subthread(T* obj, void (T::* func)());


protected:
    float audio_tempo;
    QTimer* timerLeft;
    QTimer* timerRight;
    // 新增：音量和速度的延迟隐藏定时器
    QTimer* hideVolumeTimer = nullptr;
    QTimer* hideSpeedTimer = nullptr;
    void keyPressEvent(QKeyEvent *event) override;
    void keyReleaseEvent(QKeyEvent *event) override;
    bool eventFilter(QObject* obj, QEvent* event) override;
    void closeEvent(QCloseEvent *event) override;


private slots:
    void initConnectSlots();

    void keyLeftHoldSlot();
    void keyRightHoldSlot();

    void openFileSlot(bool try_error = false);
    void onCentralWidgetContextMenuRequested();
    void initOpenFileConnectSlot();

    void audioErrorSlot(const std::string& msg);

    void playSlot();
    void pauseSlot();
    void recordingSlot();
    void resetSlot();
    void audioEndSlot();
    void seekingSlot();
    void seekToSlot();
    void setAudioPositionSlot(int64_t position);
    void currentTimeSlot();

    void setPitchSlot();
    void setRateSlot();

    void setListenerSlot();
    void setListenerXSlot();
    void setListenerYSlot();
    void setListenerZSlot();
    void show_coord_window();
    void showCoordWindowSlot();
    //void setVariableCoordSlot();
    void ChoiceButtonSlot();

    void setDensitySlot();
    void setDiffusionSlot();
    void setGainSlot();
    void setGainHFSlot();
    void setGainDecayTimeSlot();
    void setDecayHFRatioSlot();
    void setReflectionsGainSlot();
    void setReflectionsDelaySlot();
    void setLateReverbGainSlot();

    void setF62Slot(int value);
    void setF125Slot(int value);
    void setF250Slot(int value);
    void setF500Slot(int value);
    void setF1000Slot(int value);
    void setF2000Slot(int value);
    void setF4000Slot(int value);
    void setF8000Slot(int value);
    void setF16000Slot(int value);
    void resetFiltersSlot();

    //void setVolumeSlot(int volume);

    void onGroupBoxReverbContextMenuRequested(const QPoint &pos);
    void setAllCurrentReverbEffects();
    void enableAllReverbEffects();
    void disableAllReverbEffects();


private:
    //QString formatTime(double seconds);
    Ui::MyWindowClass* ui;
    CoordWindow* coord_window;
    ListenerCoordThread* my_coord_thread;
    //VolumeControl* volume_control;

    // store the input file path.
    std::string input_file;

    // create a MusicPlayer object.
    MusicPlayer* my_player;

    // save the duration of the input file.
    double file_duration;

    // 新增：用于判断 slider 是否被用户按下
    bool slider_is_pressed;

    // update the progress bar.
    QSlider* sliderVolume = nullptr;
    QLabel* labelVolume = nullptr;
    QSlider* sliderSpeed = nullptr;
    QLabel* labelSpeed = nullptr;
    // 新增：用于追踪鼠标是否在音量/速度相关控件内
    bool volumeAreaHovered = false;
    bool speedAreaHovered = false;

    // 新增：用于音频转码
    TranscodeWindow* audio_transcoder;
    //QThread* transcoder_thread;

    // 新增：用于打开 PCM 音频文件
    OpenPCMWindow* open_pcm_window;

    // 新增：用于保存音频文件
    RecordingWindow* recording_window;
    //QThread* recording_thread;
};

//struct MutexPlayer {
//    bool playOrPause;
//    MutexPlayer() {
//        if (my_player) {
//            playOrPause = my_player->is_playing;
//            my_player->pause();
//        }
//    }
//    ~MutexPlayer() {
//        if (playOrPause) {
//            if (my_player->is_playing)
//                my_player->play();
//        }
//    }
//}
